const fs=require("fs");
function  copyDir(oldDirPath,newDirPath){
  fs.readdir(oldDirPath,function (error,list){
   //遍历源文件夹
    list.forEach(item=>{
    //调用fs.stat判断是否是文件
        fs.stat(oldDirPath+"/"+item,function (error,stats){
          if(stats.isFile()){
            fs.copyFile(oldDirPath+"/"+item,newDirPath+"/"+item,function (){});
          }
          else{
            //如果是文件夹，则创建一个新文件夹，同时拷贝文件夹内容
            fs.mkdir(newDirPath+"/"+item,function (){  copyDir(oldDirPath+"/"+item,newDirPath+"/"+item,function (){});});
          }
        });
    });
  });
}

 function cpdir(oldDirPath,newDirPath){
   return new Promise(function (resolve,reject){
     fs.stat(oldDirPath,function (error,stats){
            if(stats.isFile()==true){
                  reject(error);
            }
            else{
              copyDir(oldDirPath,newDirPath,function (error){
                resolve("拷贝成功");
              });
            }
     });
   });
 }

                const p=cpdir("test1","test2");
                p.then(result=>{console.log(result);});
                p.catch(error=>{console.log("拷贝失败",error);});
